/**
 * Copyright (c) 2018-2099, Joffee.
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sailmi.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sailmi.common.controller.BaseController;
import com.sailmi.common.model.auth.AuthInfo;
import com.sailmi.common.mybatis.support.Condition;
import com.sailmi.common.mybatis.support.Query;
import com.sailmi.common.tool.api.R;
import com.sailmi.common.tool.utils.Func;
import com.sailmi.common.tool.utils.SecureUtil;
import com.sailmi.system.entity.ServiceEnterprise;
import com.sailmi.system.service.IServiceEnterpriseService;
import com.sailmi.system.vo.ServiceEnterpriseVO;
import com.sailmi.system.wrapper.ServiceEnterpriseWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权 控制器
 *
 * @author Joffee
 * @since 2025-03-14
 */
@RestController
@RequestMapping("/serviceenterprise")
@Tag(name = "服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权", description = "服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权接口")
public class ServiceEnterpriseController extends BaseController {
	@Resource
	private IServiceEnterpriseService serviceEnterpriseService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@Operation(summary = "详情", description = "传入serviceEnterprise")
	public R<ServiceEnterpriseVO> detail(@ModelAttribute ServiceEnterprise serviceEnterprise) {
		ServiceEnterprise detail = serviceEnterpriseService.getOne(Condition.getQueryWrapper(serviceEnterprise));
		return R.data(ServiceEnterpriseWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权
	 */
	@GetMapping("/list")
	@Operation(summary = "分页", description = "传入serviceEnterprise")
	public R<IPage<ServiceEnterpriseVO>> list(@ModelAttribute ServiceEnterprise serviceEnterprise, @ModelAttribute Query query) {
		IPage<ServiceEnterprise> pages = serviceEnterpriseService.page(Condition.getPage(query), Condition.getQueryWrapper(serviceEnterprise));
		return R.data(ServiceEnterpriseWrapper.build().pageVO(pages));
	}

	/**
	 * 分页 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权
	 */
	@GetMapping("/listAllByEnterprise")
	@Operation(summary = "分页", description = "传入serviceEnterprise")
	public R<List<ServiceEnterpriseVO>> listAllByEnterprise(@RequestParam("enterpriseId") Long enterpriseId) {

		List<ServiceEnterpriseVO> seList = serviceEnterpriseService.selectAllServiceEnterpriseByEnterprise(enterpriseId);
		return R.data(seList);
	}

	/**
	 * 分页 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权
	 */
	@GetMapping("/pageAllByEnterprise")
	@Operation(summary = "分页", description = "传入serviceEnterprise")
	public R<IPage<ServiceEnterpriseVO>> pageAllByEnterprise(@ModelAttribute Query query) {
		AuthInfo authInfo = SecureUtil.getAuthInfo();
		Long enterpriseId = Long.valueOf(authInfo.getEnterprise());
		IPage<ServiceEnterpriseVO> pages = serviceEnterpriseService.selectAllServiceEnterprisePageByEnterprise(Condition.getPage(query),enterpriseId);
		return R.data(pages);
	}


	/**
	 * 自定义分页 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权
	 */
	@GetMapping("/page")
	@Operation(summary = "分页", description = "传入serviceEnterprise")
	public R<IPage<ServiceEnterpriseVO>> page(@ModelAttribute ServiceEnterpriseVO serviceEnterprise,@ModelAttribute Query query) {
		IPage<ServiceEnterpriseVO> pages = serviceEnterpriseService.selectServiceEnterprisePage(Condition.getPage(query), serviceEnterprise);
		return R.data(pages);
	}

	/**
	 * 新增 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权
	 */
	@PostMapping("/save")
	@Operation(summary = "新增", description = "传入serviceEnterprise")
	public R save(@Valid @RequestBody ServiceEnterprise serviceEnterprise) {
		return R.status(serviceEnterpriseService.save(serviceEnterprise));
	}

	/**
	 * 修改 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权
	 */
	@PostMapping("/update")
	@Operation(summary = "修改", description = "传入serviceEnterprise")
	public R update(@Valid @RequestBody ServiceEnterprise serviceEnterprise) {
		return R.status(serviceEnterpriseService.updateById(serviceEnterprise));
	}

	/**
	 * 新增或修改 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权
	 */
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入serviceEnterprise")
	public R submit(@Valid @RequestBody ServiceEnterprise serviceEnterprise) {
		return R.status(serviceEnterpriseService.saveOrUpdate(serviceEnterprise));
	}

	/**
	 * 新增或修改 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权
	 */
	@PostMapping("/order")
	@Operation(summary = "新增或修改", description = "传入serviceEnterprise")
	public R order(@RequestParam("serviceId") Long serviceId,@RequestParam("orderType") Integer orderType) {
		if(orderType==-1){
			AuthInfo authInfo = SecureUtil.getAuthInfo();
			ServiceEnterprise serviceEnterprise = new ServiceEnterprise();
			serviceEnterprise.setEnterpriseId(Long.valueOf(authInfo.getEnterprise()));
			serviceEnterprise.setServiceId(serviceId);
			return R.status(serviceEnterpriseService.remove(Condition.getQueryWrapper(serviceEnterprise)));
		}else{
			AuthInfo authInfo = SecureUtil.getAuthInfo();
			ServiceEnterprise serviceEnterprise = new ServiceEnterprise();
			serviceEnterprise.setEnterpriseId(Long.valueOf(authInfo.getEnterprise()));
			serviceEnterprise.setServiceId(serviceId);
			serviceEnterpriseService.remove(Condition.getQueryWrapper(serviceEnterprise));
			serviceEnterprise.setAuthStatus((short)0);
			return R.status(serviceEnterpriseService.save(serviceEnterprise));
		}
	}

	/**
	 * 删除 服务企业授权表，表示哪个企业具有此服务的权限，此版本暂不考虑个人用户的服务授权
	 */
	@PostMapping("/remove")
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(serviceEnterpriseService.deleteLogic(Func.toLongList(ids)));
	}


}
